package com.woniu.adapter;

import com.woniu.outlet.dao.*;
import com.woniu.outlet.po.*;
import com.woniu.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Component
@Transactional
public class PersonAdapter {

    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private PotentialCustomersMapper potentialCustomersMapper;

    @Autowired
    private PersonCompanyMapper personCompanyMapper;

    @Autowired
    private PersonFamilyMapper personFamilyMapper;

    @Autowired
    private PersonExtendMapper personExtendMapper;

    @Autowired
    private PotentialMessageMapper potentialMessageMapper;


    //这个是添加正式的个人用户的信息，这个有两个情况，一个是从潜在客户哪里申请来的，申请成功就会删除这个潜在客户的信息
    //还有一种就是从别的渠道获得的正式客户，不管哪一种情况都会在添加完正式客户之后发一条短信给客户本人
    public int addpersoninfo(Person person){
        //添加person表格
        int i = personMapper.insert(person);
        //获得返回的主键
        Integer personId = person.getPersonId();
        System.out.println(personId);

        //如果是从潜在客户哪里申请来的，那就将这条的潜在客户删除
        if(person.getPcId()!=null){
            int i1 = potentialCustomersMapper.deleteByPrimaryKey(person.getPcId());
        }

        //添加personCompany表
        PersonCompany personCompany = person.getPersonCompany();
        personCompany.setPersonid(personId);
        int i1 = personCompanyMapper.addpercompany(personCompany);

        //添加personFamily表
        PersonFamily personFamily = person.getPersonFamily();
        personFamily.setPersonId(personId);
        int i2 = personFamilyMapper.insertSelective(personFamily);

        //添加personExtend表
        PersonExtend personExtend = person.getPersonExtend();
        personExtend.setPersonId(personId);
        int i3 = personExtendMapper.insertSelective(personExtend);

        //创建一个message对象封装数据到对象当中
        PotentialMessage potentialMessage = new PotentialMessage();
        potentialMessage.setPotentialmessageExchange("potentialcustomer_exchange");
        potentialMessage.setPotentialmessageKey("potentialcustomer.xxx");
        potentialMessage.setPotentialmessageCount(10);
        potentialMessage.setPotentialmessageContext(""+person.getPersonId()+","+"addpersoninfo");
        potentialMessage.setPotentialmessageStatus(0);
        //将这个message存到数据库
        potentialMessageMapper.insert(potentialMessage);

        return 0;
    }

    //修改个人用户的信息
    public int updatepersoninfo(Person person){
        int i = personMapper.updateByPrimaryKeySelective(person);
        return i;
    }


    //修改客户的扩展信息
    public int updatepersonextend(PersonExtend personExtend){
        int i = personExtendMapper.updateByPrimaryKeySelective(personExtend);
        return i;
    }

    //修改个人客户的公司信息
    public int updatepersoncompany(PersonCompany personCompany){
        int i = personCompanyMapper.updateByPrimaryKeySelective(personCompany);
        return i;
    }

    //添加个人客户的家人信息
    public int addpersonfamily(PersonFamily personFamily){
        int insert = personFamilyMapper.insert(personFamily);
        return insert;
    }

    //修改个人客户的家人信息
    public int updatepersonfamily(PersonFamily personFamily){
        int i = personFamilyMapper.updateByPrimaryKeySelective(personFamily);
        return i;
    }


}
